// $Id: media.c 100 2011-10-21 15:05:36Z cedric.shih@gmail.com $
/*
 * Copyright 2011 Cedric Shih (cedric dot shih at gmail dot com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <zcbuffer.h>

#include "log.h"

#include "media.h"

int niohttp_media_describe(struct niohttp_media *media,
		struct niohttp_headers *headers)
{
	int rc;
	struct niohttp_track *track;

	niohttp_debug("invoking %p to describe media: %s", media->describe,
			media->name);

	if ((rc = (*media->describe)(media, headers, media->cbarg))) {
		niohttp_error("failed to describe media '%s': %s",
				media->name,  strerror(rc));
		goto finally;
	}

	TAILQ_FOREACH(track, &media->tracks, entry) {
		if ((rc = niohttp_track_describe(track, headers))) {
			niohttp_error("failed to describe track '%s/%s': %s",
					media->name, track->name, strerror(rc));
			goto finally;
		}
	}

	rc = 0;

finally:
	return rc;
}

void niohttp_media_free(struct niohttp_media *media)
{
	if (!media) {
		return;
	}

	niohttp_debug("destructing media: %s", media->name);

	niohttp_track_list_clear(&media->tracks);

	if (media->name) {
		free(media->name);
	}

	free(media);
}

struct niohttp_media *niohttp_media_map_find(struct niohttp_media_map *map,
		const char *name)
{
	struct niohttp_media find;

	niohttp_debug("finding media: %s", name);

	find.name = (char *)name;

	return RB_FIND(niohttp_media_map, map, &find);
}

int niohttp_media_map_add(struct niohttp_media_map *map, const char *name,
		struct niohttp_media **ptr)
{
	int rc;
	struct niohttp_media *media;

	if (niohttp_media_map_find(map, name)) {
		rc = EEXIST;
		niohttp_error("existed: %s", name);
		goto finally;
	}

	niohttp_debug("adding media: %s", name);

	if (!(media = (struct niohttp_media *)calloc(1, sizeof(*media)))) {
		rc = errno ? errno : -1;
		niohttp_error("calloc: %s", strerror(rc));
		goto finally;
	}

	TAILQ_INIT(&media->tracks);

	if (!(media->name = strdup(name))) {
		rc = errno ? errno : -1;
		niohttp_error("strdup: %s", strerror(rc));
		goto finally;
	}

	RB_INSERT(niohttp_media_map, map, media);
	*ptr = media;
	rc = 0;

finally:
	return rc;
}

void niohttp_media_map_remove(struct niohttp_media_map *map,
		struct niohttp_media *media)
{
	niohttp_debug("removing media: %p", media);

	RB_REMOVE(niohttp_media_map, map, media);
	niohttp_media_free(media);
}

void niohttp_media_map_clear(struct niohttp_media_map *map)
{
	struct niohttp_media *media;

	while ((media = RB_MIN(niohttp_media_map, map))) {
		niohttp_media_map_remove(map, media);
	}
}

int niohttp_media_compare(struct niohttp_media *a, struct niohttp_media *b)
{
	return strcmp(a->name, b->name);
}

RB_GENERATE(niohttp_media_map, niohttp_media, entry, niohttp_media_compare);
